package se.hh.lab.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import se.hh.lab.dao.DaoException;
import se.hh.lab.dao.UserLoginDao;
import se.hh.lab.domain.Login;
import se.hh.lab.jdbc.JdbcUtil;

public class UserLoginDaoImpl implements UserLoginDao
{
	@SuppressWarnings("finally")
	@Override
	public int addUserLogin(Login login) 
	{
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		int mark=0;
		try
		{
			con=JdbcUtil.getConnection();
			String sql="insert into lab_userlogin(userID,username,password) values(?,?,?)";
			ps=con.prepareStatement(sql);		
			ps.setString(2, login.getLoginID());
			ps.setString(3, login.getPassword());
			ps.setString(1, login.getUserID());			
			mark=ps.executeUpdate();
		}
		catch(Exception e)
		{
			throw new DaoException(e.getMessage(),e);
		}
		finally
		{
			try
			{
				JdbcUtil.free(rs, ps, con);
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return mark;
		}

	}

	@SuppressWarnings("finally")
	@Override
	public int deleteUserLogin(Login login) 
	{
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		int mark=0;
		try
		{
			con=JdbcUtil.getConnection();
			String sql="delete from lab_userlogin where userID=?";
			ps=con.prepareStatement(sql);
			ps.setString(1, login.getUserID());
			mark=ps.executeUpdate();			
		}
		catch(Exception e)
		{
			throw new DaoException(e.getMessage(), e);
		}
		finally
		{
			try
			{
				JdbcUtil.free(rs, ps, con);
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return mark;
		}
	}

	@SuppressWarnings("finally")
	@Override
	public int updateUserLogin(Login login) 
	{
		Connection con=null;
		ResultSet rs=null;
		PreparedStatement ps=null;
		int k=0;
		try
		{
			con=JdbcUtil.getConnection();
			String sql="update lab_userlogin set password='"+login.getPassword()+"' where userID='"+login.getUserID()+"'";
			System.out.println(sql);
			Statement stmt = con.createStatement();
			k = stmt.executeUpdate(sql);
		}
		catch(Exception e)
		{
			throw new DaoException(e.getMessage(),e);
		}
		finally
		{
			try
			{
				JdbcUtil.free(rs, ps, con);
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
			return k;
		}
		
	}

	@SuppressWarnings("finally")
	@Override
	public ArrayList<Login> getUserLogin() 
	{
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;		
		ArrayList<Login> list=new ArrayList<Login>();
		
		try
		{
			con=JdbcUtil.getConnection();
			String sql="select userID,username,password from lab_userlogin";
			ps=con.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next())
			{
				Login login=new Login();
				login.setLoginID(rs.getString("username"));
				login.setUserID(rs.getString("userID"));
				login.setPassword(rs.getString("password"));
				list.add(login);
			}
		}
		catch(Exception e)
		{
			throw new DaoException(e.getMessage(),e);
		}
		finally
		{
			try
			{
				JdbcUtil.free(rs, ps, con);
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return list;			
		}		
		
	}

	@SuppressWarnings("finally")
	@Override
	public int getUserLogin(Login login) 
	{
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;		
		int flag = 0;
		try
		{
			
			con=JdbcUtil.getConnection();
			String sql="select userID from lab_userlogin where userID=? and password=?";
			ps=con.prepareStatement(sql);
			ps.setString(1, login.getUserID());
			ps.setString(2, login.getPassword());
			rs=ps.executeQuery();
			while(rs.next())
			{
				flag = 1;
			}
			
		}
		catch(Exception e)
		{
			throw new DaoException(e.getMessage(),e);
		}
		finally
		{
			try
			{
				JdbcUtil.free(rs, ps, con);
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return flag;			
		}		
				
	}

}
